/*
 * AddTransitionCommand.java
 *
 * Created on 9 czerwiec 2007, 13:24
 */

package edu.uj.petri.commands;

import edu.uj.petri.document.Document;
import edu.uj.petri.model.Model;
import edu.uj.petri.model.Transition;
import edu.uj.petri.view.TransitionView;
import edu.uj.petri.view.View;
import java.awt.geom.Point2D;
import java.util.ResourceBundle;

/**
 *
 * @author Paweł
 */
public class AddTransitionCommand extends Command {
    
    private TransitionView transitionView;
    private Transition transition;
    private Document document;
    private Point2D position;
    private boolean firstTime=true;
    
    public AddTransitionCommand(Document document, Point2D position ) {
        
        this.document = document;
        this.position = position;
    }

    public String toString() {
        String retval=ResourceBundle.getBundle("edu/uj/petri/messages").getString("dodanie_tranzycji");
        if (transition != null) {
            retval += " " + transition.getName();
        }
        return retval;
    }
    
    void execute() {
        Model model = document.getModel();
        View view = document.getView();
        
        if (firstTime) {
            transition = model.createTransition();
            transitionView = new TransitionView( transition, position );
            firstTime=false;
        }
        
        model.addTransition(transition); // TODO OK? - by Piotrek
        view.addElement( transitionView );
        view.draw();
    }

    void undo() {
        Model model = Document.getActiveDocument().getModel();
        View view = document.getView();
        
        model.deleteTransition(transition);
        view.removeElement( transitionView );
        view.draw();
    }

    boolean canBeCombined(Command c) {
        
        return false;
    }

    private static final long serialVersionUID = 5751999055235889400L;
}